<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from 'echarts'
import resize from './mixins/resize'

export default {
  name: 'BarChart',
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart',
    },
    width: {
      type: String,
      default: '100%',
    },
    height: {
      type: String,
      default: '200px',
    },
    chartData: {
      type: Array,
      default: () => {
        return []
      },
    },
  },
  data() {
    return {
      chart: null,
      title: [],
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  computed: {
    // eslint-disable-next-line
    totalCount: function () {
      const count = 10
      /* if (this.chartData.length) {
        count = this._.sumBy(this.chartData, o => {
          return o.value
        })
      } */
      return count
    },
    dateData: function () {
      return this.chartData.map(e => e.name)
    },
    valueData: function () {
      return this.chartData.map(e => e.number)
    },
  },
  watch: {
    chartData: {
      handler(newVal, oldVal) {
        this.title = newVal.map(item => item.name)
        this.initChart()
      },
      deep: true,
      immediate: false,
    },
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      if (!this.chart) {
        this.chart = echarts.init(this.$el, 'macarons')
      }
      this.chart.setOption({
        /*  title: {
          text: this.totalCount,
          subtext: '总量',
          x: 'center',
          y: '36%',
          textStyle: {
            fontWeight: 'bolder',
            fontSize: 26,
            color: '#333',
          }, // 标题
          subtextStyle: {
            fontWeight: 'bolder',
            fontSize: 14,
            color: '#333',
          }, // 副标题
        }, */
        title: {
          text: '帮扶政策分类统计',
          x: 'center',
          textStyle: {
            fontFamily: 'syht-Bold',
            fontSize: 16,
            fontWeight: 700,
            color: '#333',
          },
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow',
          },
          textStyle: {
            fontFamily: 'syht-Regular',
            fontSize: 14,
            fontWeight: 400,
            color: '#333',
          },
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true,
        },
        xAxis: [
          {
            type: 'category',
            data: this.dateData,
            axisTick: {
              alignWithLabel: true,
            },
            axisLabel: {
              show: true,
              interval: 0,
              textStyle: {
                fontFamily: 'syht-Regular',
                fontSize: 12,
                fontWeight: 400,
                color: '#666',
              },
              // 文字换行
              formatter: function (value) {
                let res = '' // 拼接加\n返回的类目项
                const maxLength = 3 // 每项显示文字个数  数字设置几，就一行显示几个文字
                const valLength = value.length // X轴上的文字个数
                const rowN = Math.ceil(valLength / maxLength) // 需要换行的行数
                // 换行的行数大于1,
                if (valLength > 4) {
                  for (let i = 0; i < rowN; i++) {
                    let temp = ''
                    const start = i * maxLength
                    const end = start + maxLength
                    temp = value.substring(start, end) + '\n'
                    res += temp
                  }
                  return res
                } else {
                  return value
                }
              },
            },
          },
        ],
        yAxis: [
          {
            type: 'value',
            minInterval: 1,
            axisLabel: {
              textStyle: {
                fontFamily: 'syht-Regular',
                fontSize: 12,
                fontWeight: 400,
                color: '#666',
              },
            },
          },
        ],
        series: [
          {
            name: '帮扶政策分类统计',
            type: 'bar',
            barWidth: 20,
            data: this.valueData,
            textStyle: {
              fontFamily: 'syht-Regular',
              fontSize: 16,
              color: '#333',
            },
          },
        ],
      })
    },
  },
}
</script>
<style lang="scss" scoped>
.chart {
  width: 100%;
  height: 100%;
}
</style>
